package com.atues.stack;

import java.util.Stack;
import java.util.TreeMap;

public class MinStack {
    //辅助栈；

    Stack<Integer> stack ;
    Stack<Integer> minStack;//最小辅助栈，用来保存最小值，只需要保持与原来的栈同进同出就好了；
    //这样栈顶的值其实肯定就是最小值了；
    public MinStack() {
        minStack = new Stack<>();
        stack = new Stack<>();
    }

    public void push(int val) {//这里可以优化一下，最小栈的插入
        stack.push(val);
        if(minStack.isEmpty() || minStack.peek() >= val){//如果当前值比最小栈的栈顶值小或者等于，那么就可以直接放入最小栈中；
            minStack.push(val);
        }
    }

    public void pop() {

        if(stack.pop() == minStack.peek()){//如果当前值是最小值，那么就需要将最小栈的栈顶值也弹出；
            minStack.pop();
        }


    }

    public int top() {

        return stack.peek();
    }

    public int getMin() {
        return minStack.peek();
    }

    public static void main(String[] args) {
        MinStack minStack = new MinStack();
//        minStack.push(-2);
////        minStack.pop();
//        minStack.push(0);
//        minStack.push(-3);
//        System.out.println(minStack.getMin());
//        minStack.pop();
//        System.out.println(minStack.top());
////        minStack.pop();
//        System.out.println(minStack.getMin());



        Integer a = 127;
        Integer b = 127;
        Integer c = 128;
        Integer d = 128;

        System.out.println(a == b);//true
        System.out.println(c == d);//false
    }
}
